﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MediaManager
{
    public partial class AddUserFrm : Form
    {
        private AddUserFrm()
        {
            InitializeComponent();
        }
        public AddUserFrm(string userName)
        {
            InitializeComponent();
            _userName = userName;
        }
        private string _userName = "";
        private void AddUserFrm_Load(object sender, EventArgs e)
        {
            Init();
        }
        private void Init()
        {
            txtName.Text = "";
            txtPassword.Text = "";
            txtPassword1.Text = "";
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Length < 1)
                {
                    GlobalBase.Global.Instance.ShowMessage("请输入用户名！");
                    txtName.Focus();
                    return;
                }
                if (txtPassword.Text.Length < 1 || txtPassword1.Text.Length < 1)
                {
                    GlobalBase.Global.Instance.ShowMessage("请输入密码！");
                    return;
                }
                if (txtPassword.Text == txtPassword1.Text)
                {
                    Login login = null;
                    try
                    {
                        login = _dataContext.Login.Where(p => p.name == txtName.Text).Single();
                    }
                    catch
                    {
                        login = null;
                    }
                    if (null != login)
                    {
                        GlobalBase.Global.Instance.ShowMessage("用户已存在！");
                        return;
                    }
                    login = new Login();
                    login.id = _dataContext.GetLoginMaxIndex();
                    login.name = txtName.Text;
                    login.password = Crypt.Cryption.Instance.EncryptDES(txtPassword.Text);
                    login.创建日期 = DateTime.Now;
                    _dataContext.Login.InsertOnSubmit(login);
                    _dataContext.SubmitChanges();

                    string ss = "添加新用户：{0}成功！";
                    ss = string.Format(ss, login.name);
                    GlobalBase.Global.Instance.ShowMessage(ss);
                    string s = string.Format(StructDayInfo.添加人员, txtName.Text, DateTime.Now); 
                    InserDayInfo(s);
                    Init();

                }
                else
                {
                    GlobalBase.Global.Instance.ShowMessage("密码与确认密码不匹配！");
                    return;
                }
            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage("添加新用户失败！");
            }
        }
        private void InserDayInfo(string dInfo)
        {
            DayInfo dayInfo = new DayInfo();
            dayInfo.id = _dataContext.GetDayInfoIndex();
            dayInfo.name = _userName;
            dayInfo.操作 = dInfo;
            dayInfo.日期 = DateTime.Now;
            _dataContext.DayInfo.InsertOnSubmit(dayInfo);
            _dataContext.SubmitChanges();

        }
        DataClasses2DataContext _dataContext = new DataClasses2DataContext();
    }
}
